from math import exp
import pathlib
import time
import re
import typing
import os
import yaml
import ctypes
import psutil
import subprocess
import requests
import winsound
import datetime
import rich
from selenium.webdriver.edge.webdriver import WebDriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import *







def print_with_time(*args, **kwargs):
    """在输出内容前添加当前时间戳，使用rich.print进行格式化输出
    
    功能：在所有输出内容前自动添加当前本地时间，时间格式为YYYY-MM-DD HH:MM:SS
    时间会以蓝色加粗样式显示，其余内容保持rich的默认格式化特性
    
    Args:
        *args: 任意数量的位置参数，将被原样传递给rich.print
        **kwargs: 任意关键字参数，将被原样传递给rich.print
    
    Example:
        >>> print_with_time("系统启动完成")
        [bold blue][2025-08-07 16:45:30][/bold blue] 系统启动完成
        
        >>> print_with_time("操作结果：", "[green]成功[/green]")
        [bold blue][2025-08-07 16:46:10][/bold blue] 操作结果：[green]成功[/green]
    """
    # 获取当前时间并格式化为字符串
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    # 拼接时间前缀与输出内容并打印
    rich.print(f"[bold blue][{current_time}][/bold blue]", *args, **kwargs)








def On_InputPage_SelectSizeChat(driver:WebDriver,SizeCharType:str,SizeCharList:list):
    """选择尺码类型以及尺码

    Args:
        driver (WebDriver): _description_
        wait (WebDriverWait): _description_
        actions (ActionChains): _description_
        SizeCharType (str): 尺码类型
        SizeCharList (list): 尺码列表
    """
    # 1.打开尺码表选择框
    try:
        element = WebDriverWait(driver=driver,timeout=10).until(expected_conditions.visibility_of_element_located(locator=(By.XPATH,f"//input[@placeholder='请选择尺码']")))
        driver.execute_script("arguments[0].scrollIntoView({block: 'center', inline: 'center'});",element)
        try:
            element.click()
            try:
                element = WebDriverWait(driver=driver,timeout=10).until(expected_conditions.visibility_of_element_located((By.XPATH,f"//ul/li/span[text()='{SizeCharType}' and @title='{SizeCharType}']")))
                element.click()
            except:
                print_with_time(f"提示 : 打开 [尺码表] 选择框失败!")
            
        except (ElementClickInterceptedException,ElementNotInteractableException):
            driver.execute_script("arguments[0].click();",element)            
    except TimeoutException:
        print_with_time(f"提示 : 没有找到选择 [尺码表] 输入框!")

    # 2.选择尺码
    try:
        element = WebDriverWait(driver=driver,timeout=10).until(expected_conditions.presence_of_element_located((By.XPATH,f"//ul/li/span[text()='{SizeCharType}' and @title='{SizeCharType}']"))) 
        try:
            element.click()
        except (ElementClickInterceptedException,ElementNotInteractableException):
            driver.execute_script("arguments[0].click();",element)        
    except TimeoutException:
        print_with_time(f"提示 : 没有找到选择 [尺码类型] 的选项!")
    
    
    # 3.选择尺码列表
    for sizechar in SizeCharList:
        try:
            element = WebDriverWait(driver=driver,timeout=10).until(expected_conditions.presence_of_element_located((By.XPATH,f"//label[.//input[@type='checkbox'] and .//span[text()='{sizechar}']]//input[@type='checkbox']")))
            try:
                element.click()
            except (ElementClickInterceptedException,ElementNotInteractableException):
                driver.execute_script("arguments[0].click();",element)        
        except TimeoutException:
            print_with_time(f"提示 : 没有找打 {sizechar} 尺码,不做选择!直接跳过当前尺码!")


    # 4.确认尺码返回
    try:
        element = WebDriverWait(driver=driver,timeout=10).until(expected_conditions.visibility_of_element_located((By.XPATH,f"//button/span[text()='确定']")))
        try:
            element.click()
        except (ElementClickInterceptedException,ElementNotInteractableException):
            driver.execute_script("arguments[0].click();",element)
    except TimeoutException:
        print_with_time(f"提示 : 没有找到 [确定] 按钮!")


    # 5.等待是否可能出现的弹窗
    try:
        element = WebDriverWait(driver=driver,timeout=10).until(expected_conditions.visibility_of_element_located((By.XPATH,f"//div[contains(@class,'dialog')]/button/span[text()='确定']")))
        try:
            element.click()
        except (ElementClickInterceptedException,ElementNotInteractableException):
            driver.execute_script("arguments[0].click();",element)
        
    except TimeoutException:
        print_with_time(f"提示 : 没有找到 [尺码表] 选择框!")


